
from applications.extensions import db
from datetime import datetime

# 频道models
class admin_iptvadmin_channel(db.Model):
    __tablename__ = 'admin_iptv_channel'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='频道ID')
    name = db.Column(db.String(128), nullable=False, comment='频道名称')
    category = db.Column(db.String(32), nullable=False, comment='频道分类')
    url = db.Column(db.String(512), nullable=False, comment='播放地址')
    create_time = db.Column(db.DateTime, nullable=False, default=datetime.now, comment='创建时间')

    def __repr__(self):
        return f'<Channel {self.name}>'

# 分类models
class admin_iptvadmin_category(db.Model):
    __tablename__ = 'admin_iptv_category'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(128), nullable=False, comment='分类名称')
    sort = db.Column(db.Integer, default=0, comment='排序')
    create_time = db.Column(db.DateTime, default=datetime.now, comment='创建时间')

# rss订阅models
class admin_iptvadmin_rss(db.Model):
    __tablename__ = 'admin_iptv_rss'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), nullable=False, comment='订阅名称')
    type = db.Column(db.String(10), nullable=False, comment='订阅类型')
    url = db.Column(db.String(500), nullable=False, comment='订阅地址')
    category = db.Column(db.String(100), nullable=False, comment='订阅分类')
    status = db.Column(db.Boolean, default=True, comment='状态')
    encrypt = db.Column(db.Boolean, default=False, comment='是否加密')
    sort = db.Column(db.Integer, default=0, comment='排序')
    # update_interval = db.Column(db.Integer, default=24, comment='更新间隔(小时)')
    last_update = db.Column(db.DateTime, comment='最后更新时间')
    enable = db.Column(db.Boolean, default=True, comment='是否启用')
    remark = db.Column(db.String(500), comment='备注')
    create_time = db.Column(db.DateTime, default=datetime.now, comment='创建时间')
    update_time = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now, comment='更新时间')

    def to_dict(self):
        return {
            'id': self.id, # 频道ID
            'name': self.name, # 频道名称
            'type': self.type, # 频道类型
            'url': self.url, # 播放地址
            'category': self.category, # 频道分类
            'status': self.status, # 状态
            'encrypt': self.encrypt, # 是否加密
            'sort': self.sort, # 排序
            # 'update_interval': self.update_interval, # 更新间隔
            'last_update': self.last_update.strftime('%Y-%m-%d %H:%M:%S') if self.last_update else None, # 最后更新时间
            'enable': self.enable, # 是否启用
            'remark': self.remark, # 备注
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S'), # 创建时间
            'update_time': self.update_time.strftime('%Y-%m-%d %H:%M:%S') # 更新时间
        }

# 客户端上线日志models
class admin_iptvadmin_client_log(db.Model):
    __tablename__ = 'admin_iptv_client_log'
    id = db.Column(db.Integer, primary_key=True)
    client_id = db.Column(db.String(100), nullable=False, comment='客户端ID')
    client_version = db.Column(db.String(50), comment='客户端版本')
    client_ip = db.Column(db.String(50), comment='客户端IP')
    client_model = db.Column(db.String(100), comment='设备型号')
    client_system = db.Column(db.String(100), comment='系统版本')
    last_login = db.Column(db.DateTime, default=datetime.now, comment='最后登录时间')
    create_time = db.Column(db.DateTime, default=datetime.now, comment='创建时间')

# 客户端设备表models
class admin_iptvadmin_client(db.Model):
    """客户端设备表"""
    '''
    {
    "deviceBrand":"OnePlus",
    "deviceName":"HD1910",
    "deviceOsVer":"9",
    "deviceMac":"00:DB:25:1F:33:36",
    "sdkVer":28,
    "androidId":"99dfb46e741830eb",  
    "apkAppName":"天光云影",
    "apkPackageName":"com.chinablue.tv",
    "apkVer":"3.3.7","t":1736179119,
    "sign":"3da4c6abdd46674cb017f89bc6bc8bdd"
    }
    '''  
    __tablename__ = 'admin_iptv_client'
    
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='主键ID')
    androidId = db.Column(db.String(100), nullable=True, comment='安卓ID')
    deviceBrand = db.Column(db.String(100), nullable=True, comment='设备品牌')
    deviceName = db.Column(db.String(100), nullable=True, comment='设备名称')
    deviceOsVer = db.Column(db.String(100), nullable=True, comment='设备系统版本')
    deviceMac = db.Column(db.String(100), nullable=True, comment='设备MAC')
    sdkVer = db.Column(db.String(100), nullable=True, comment='SDK版本')
    apkAppName = db.Column(db.String(100), nullable=True, comment='APP名称')
    apkPackageName = db.Column(db.String(100), nullable=True, comment='APP包名')
    apkVer = db.Column(db.String(100), nullable=True, comment='APP版本')
    sign = db.Column(db.String(100), nullable=True, comment='签名')
    authorize = db.Column(db.Boolean, default=False, comment='授权状态')
    config_file = db.Column(db.String(100), nullable=True, default='default.json', comment='配置文件') 
    create_time = db.Column(db.DateTime, default=datetime.now, comment='创建时间')
    update_time = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now, comment='更新时间')
    

